﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;
namespace QLTTTH
{
    public partial class UCDiemThi : DevExpress.XtraEditors.XtraUserControl
    {
        public UCDiemThi()
        {
            InitializeComponent();
        }

        private void UCDiemThi_Load(object sender, EventArgs e)
        {
            LoadDiemThi();
            grvDiemthi.OptionsSelection.MultiSelect = true;
        }
        private void LoadDiemThi()
        {
            var aResult = DiemThiManager.LayDanhSachDiemThi();
            if (aResult.Success)
                gridDanhsach.DataSource = aResult.Result;
            //grvDSChungChi.DataSource = aResult.Result;
        }

        private void btnTimkiem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grvDiemthi.ShowFindPanel();
        }





        private void btnXemchitiet_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
                frmDiemThi frm = new frmDiemThi();
                frm.FillControl(LayDiemThi_GrvDanhSach());
                frm.btnLuu.Enabled = false;
                frm.ShowDialog();
        }
        private DiemThi LayDiemThi_GrvDanhSach()
        {
            var dt = new DiemThi();
            dt.MaKH = int.Parse(grvDiemthi.GetFocusedRowCellValue("MaKH").ToString());
            dt.MaHV = int.Parse(grvDiemthi.GetFocusedRowCellValue("MaHV").ToString());
            dt.MaKyThi = int.Parse(grvDiemthi.GetFocusedRowCellValue("MaKyThi").ToString());
            dt.DiemLyThuyet = float.Parse(grvDiemthi.GetFocusedRowCellValue("DiemLyThuyet").ToString());
            dt.DiemThucHanh = float.Parse(grvDiemthi.GetFocusedRowCellValue("DiemThucHanh").ToString());
            dt.KetQua = grvDiemthi.GetFocusedRowCellValue("KetQua").ToString();
            return dt;
        }

        private void btnThem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmDiemThi frm = new frmDiemThi();
            frm.sendData = new frmDiemThi.SendData(ThemDiemThi);
            frm.ShowDialog();
        }

        private void ThemDiemThi(DiemThi diemthi)
        {
            if (DiemThiManager.ThemDiemThi(diemthi).Success)
            {
                LoadDiemThi();
            }
        }

        private void btnXoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("Bạn chắc chắn xóa điểm thi này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                int maKyThi, maKH, maHV;

                for (int i = 0; i < grvDiemthi.RowCount; i++)
                {
                    if (grvDiemthi.IsRowSelected(i))
                    {
                        maKyThi = int.Parse(grvDiemthi.GetRowCellDisplayText(i, "MaKyThi").ToString());
                        maKH = int.Parse(grvDiemthi.GetRowCellDisplayText(i, "MaKH").ToString());
                        maHV = int.Parse(grvDiemthi.GetRowCellDisplayText(i, "MaHV").ToString());
                        DiemThiManager.XoaDiemThi(maKyThi, maKH, maHV);
                    }
                }

                grvDiemthi.DeleteSelectedRows();

            }
        }



        private void CapNhatDiemThi(DiemThi dt)
        {
            DiemThiManager.CapNhatDiemThi(dt);
            grvDiemthi.SetFocusedRowCellValue("MaHV", dt.MaHV);
            grvDiemthi.SetFocusedRowCellValue("MaKyThi", dt.MaKyThi);
            grvDiemthi.SetFocusedRowCellValue("DiemLyThuyet", dt.DiemLyThuyet);
            grvDiemthi.SetFocusedRowCellValue("DiemThucHanh", dt.DiemThucHanh);
            grvDiemthi.SetFocusedRowCellValue("KetQua", dt.KetQua);
        }
        private void btnCapnhat_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmDiemThi frm = new frmDiemThi();
            frm.FillControl(LayDiemThi_GrvDanhSach());
            frm.sendData = new frmDiemThi.SendData(CapNhatDiemThi);
            frm.ShowDialog();
        }
    }
}
